package com.zmn.plat.admin.controller.channel.promotion;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.channel.promotion.ChannelPromotion;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 渠道推广配置(当前只有今日头条使用)
 *
 * @author linfeng
 *
 * @since 2020/11/20 17:03
 */
@RestController
@RequestMapping("/channel/promotion/")
public class ChannelPromotionModifyController extends ChannelPromotionBaseController {

    /**
     * 新增修改
     * 
     * @author linfeng
     * @param channelPromotion
     * @since 2020/11/20 17:03
     */
    @RequestMapping(value = "addEdit")
    @RequiresPermissions("/channel/promotion/detail.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ChannelPromotion channelPromotion) {
        try {
            Integer relateId = channelPromotionBService.addEditChannePromotion(channelPromotion);

            return ResponseDTO.success(relateId, "操作成功");
        } catch (PlatException ex) {
            logger.error(ex.getMessage());
            return ResponseDTO.fail(ex.getMessage());
        } catch (Exception ex) {
            logger.error("[zmn]新增/修改渠道推广配置失败, channelPromotion:{}", channelPromotion, ex);
            return ResponseDTO.fail("操作失败");
        }
    }

    /**
     * 修改状态
     * 
     * @author linfeng
     * @param ids
     * @param status
     * @since 2020/11/20 17:03
     */
    @RequestMapping(value = "updateStatus")
    @RequiresPermissions("/channel/promotion/updateStatus.action")
    public ResponseDTO updateStatus(@RequestParam(value = "ids", defaultValue = "") String ids, @RequestParam(value = "status", defaultValue = "0") Integer status) {
        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }
        if (status == 0) {
            return ResponseDTO.fail("请设置状态！");
        }

        try {
            channelPromotionBService.updateStatus(ids, status);
            return ResponseDTO.success(true,"操作成功！");
        } catch (PlatException e){
          return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            return ResponseDTO.fail("操作失败");
        }
    }
}
